package com.oscar.ehome.view.pageview;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/14
 */

public class HtmlActivity extends BasesActivity {

    /**
     *
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     *
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     *
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.html_webview)
    WebView htmlWebview;

    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;

    String eventHtml = "";
    String name = "";
    String str = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html);
        ButterKnife.bind(this);
        setColor();
        initView();
        if (eventHtml != null) {
            str = setView(eventHtml);
        }
        loadWebView();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(HtmlActivity.this).getSystemConfig().getColor()));
    }

    public void initView() {
        name = getIntent().getStringExtra("name");
        eventHtml = getIntent().getStringExtra("eventHtml");

        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText(name);
    }

    public String setView(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                "<style>img{max-width: 80%; width:auto; height:auto;}</style>" +
                "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    public void loadWebView() {
//        showLoading();
        // WebView加载web资源
        WebSettings webSettings = htmlWebview.getSettings();
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片

        htmlWebview.setWebViewClient(new WebViewClient());
        htmlWebview.loadDataWithBaseURL(null, str, "text/html", "utf-8", null);
    }


    @OnClick(R.id.title_layout_left)
    public void onViewClicked() {
        finish();
    }
}
